// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import BrowseProducts from '../views/BrowseProducts.vue'
import AddProduct from '../views/AddProduct.vue'
import ProfileUpdate from "@/views/ProfileUpdate.vue";
import Settings from '@/views/Settings.vue'
import AccountSecurity from "@/views/accountSecurity.vue";
import ProductDetails from '../views/ProductDetails.vue'
import AuthPage from '@/components/AuthPage.vue'
import LoginForm from '@/components/LoginForm.vue'
import RegisterForm from '@/components/RegisterForm.vue'
import FineBi from "@/views/FineBi.vue";

const routes = [
    {
        path: '/',
        name: 'Auth',
        component: AuthPage,
        children: [
            {
                path: '',
                name: 'Login',
                component: LoginForm
            },
            {
                path: 'register',
                name: 'Register',
                component: RegisterForm
            }
        ]
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: "/home/browse",
        meta: { requiresAuth: true },
        children: [
            {
                path: 'browse',
                name: 'BrowseProducts',
                component: BrowseProducts
            },
            {
                path: 'add',
                name: 'AddProduct',
                component: AddProduct
            },
            { path: 'settings', name: 'Settings', component: Settings },
            { path: 'profileUpdate', name: 'ProfileUpdate', component: ProfileUpdate, meta: {showBackButton: true} },
            { path: 'finebi', name: 'FineBi', component: FineBi, meta: {showBackButton: true} },
            { path: 'accountSecurity', name: 'AccountSecurity', component: AccountSecurity, meta: {showBackButton: true} },
            {path: 'changePassword', name: 'ChangePassword', component: () => import('@/views/ChangePassword.vue'), meta: {showBackButton: true}},
            {
                path: 'product/:id',
                name: 'ProductDetails',
                component: ProductDetails,
                meta: { showBackButton: true }
            }
        ]
    },
    {
        path: '/test',
        name: 'Test',
        component: () => import('../views/Test.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 简单的身份验证逻辑（需根据实际情况调整）
// router.beforeEach((to, from, next) => {
//     const isAuthenticated = !!localStorage.getItem('authToken') // 假设使用 token 进行认证
//     if (to.meta.requiresAuth && !isAuthenticated) {
//         next({ name: 'Login' })
//     } else {
//         next()
//     }
// })

export default router
